package com.stylefeng.guns.modular.stockmanagement.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.dto.GoodsDamageAddRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.GoodsDamage;
import com.stylefeng.guns.modular.stockmanagement.service.IGoodsDamageService;

import java.util.Date;

/**
 * 商品报损控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 21:36:43
 */
@Controller
@RequestMapping("/goodsDamage")
public class GoodsDamageController extends BaseController {

    private String PREFIX = "/stockmanagement/goodsDamage/";

    @Autowired
    private IGoodsDamageService goodsDamageService;

    /**
     * 跳转到商品报损首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String damageOrderNumber = "BS-" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        model.addAttribute("damageOrderNumber",damageOrderNumber);
        return PREFIX + "goodsDamage.html";
    }

    /**
     * 跳转到添加商品报损
     */
    @RequestMapping("/goodsDamage_add")
    public String goodsDamageAdd() {
        return PREFIX + "goodsDamage_add.html";
    }

    /**
     * 跳转到修改商品报损
     */
    @RequestMapping("/goodsDamage_update/{goodsDamageId}")
    public String goodsDamageUpdate(@PathVariable Integer goodsDamageId, Model model) {
        GoodsDamage goodsDamage = goodsDamageService.selectById(goodsDamageId);
        model.addAttribute("item",goodsDamage);
        LogObjectHolder.me().set(goodsDamage);
        return PREFIX + "goodsDamage_edit.html";
    }

    /**
     * 获取商品报损列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return goodsDamageService.selectList(null);
    }

    /**
     * 新增商品报损
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody GoodsDamageAddRequest request) {
        boolean success = goodsDamageService.insertDamageListAndGoods(request);
        if(success){
            String returnSaleOrderNumber = "BS-" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
            return SUCCESS_TIP.setResultData(returnSaleOrderNumber);
        }
        return new ErrorTip(500, "增加商品报损清单!");
    }

    /**
     * 删除商品报损
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer goodsDamageId) {
        goodsDamageService.deleteById(goodsDamageId);
        return SUCCESS_TIP;
    }

    /**
     * 修改商品报损
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(GoodsDamage goodsDamage) {
        goodsDamageService.updateById(goodsDamage);
        return SUCCESS_TIP;
    }

    /**
     * 商品报损详情
     */
    @RequestMapping(value = "/detail/{goodsDamageId}")
    @ResponseBody
    public Object detail(@PathVariable("goodsDamageId") Integer goodsDamageId) {
        return goodsDamageService.selectById(goodsDamageId);
    }
}
